package factorytest.iflytek.com.einkiflytekfactorytest.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import androidx.annotation.NonNull;

import factorytest.iflytek.com.einkiflytekfactorytest.config.EinkProperty;
import factorytest.iflytek.com.einkiflytekfactorytest.config.HtfyunBuildConfig;

/**
 * Created by sst on 2018/1/11.
 */

public class UsefulUtils {

    private static final ThreadLocal<SimpleDateFormat> SDF_THREAD_LOCAL = new ThreadLocal<>();

    @NonNull
    private static SimpleDateFormat getDefaultFormat() {
        SimpleDateFormat simpleDateFormat = SDF_THREAD_LOCAL.get();
        if (simpleDateFormat == null) {
            simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.getDefault());
            SDF_THREAD_LOCAL.set(simpleDateFormat);
        }
        return simpleDateFormat;
    }

    public static String now2String() {
        return getDefaultFormat().format(new Date());
    }

    public static String millis2String(final long millis) {
        return millis2String(millis, getDefaultFormat());
    }

    /**
     * Milliseconds to the formatted time string.
     *
     * @param millis The milliseconds.
     * @param format The format.
     * @return the formatted time string
     */
    public static String millis2String(final long millis, @NonNull final DateFormat format) {
        return format.format(new Date(millis));
    }


    public static String getAppVersion(Context context) {
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            return info.versionName;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "0.0.0";
    }

    /**
     * 获取运行内存实际容量(KB)
     * 返回是 GB 值
     */
    public static double getRamTotalSize() {

        double memorySize = 0.0f;//1 * 1024 * 1024;//1GB;
        BufferedReader bufferedReader = null;
        File file = new File("/proc/meminfo");
        if (file.exists()) {
            try {
                bufferedReader = new BufferedReader(new FileReader("/proc/meminfo"), 8192);
                String line = null;
                String content = null;
                while ((line = bufferedReader.readLine()) != null) {
                    if (line.contains("MemTotal:")) {
                        content = line.substring(line.indexOf("MemTotal:"));
                        memorySize = Double.parseDouble(content.replaceAll("\\D+", ""));
                        //Log.i(TAG, "get memory size: " + memorySize + " KB");
                        break;
                    }
                }
            } catch (IOException e) {

            } finally {
                if (bufferedReader != null) {
                    try {
                        bufferedReader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
//        DebugLog.e("memorySize = " + memorySize);

        return memorySize / 1024 / 1024;

    }

    public static String getRamNominalSize() {
        String size = String.format("%.2f", (getRamTotalSize()));
        return size + " GB";
    }


    //返回的是 GB 值
    public static double getRomTotalSize() {

        double memorySize = 0.0f;//1 * 1024 * 1024;//1GB;
        BufferedReader bufferedReader = null;
        File file = new File("/proc/partitions");
        String mmcblk = "mmcblk0";
        if (EinkProperty.isRK3326Platform()) {
            mmcblk = "mmcblk1";
        }
        if (file.exists()) {
            try {
                bufferedReader = new BufferedReader(new FileReader("/proc/partitions"), 8192);
                String line = null;
                String content = null;
                while ((line = bufferedReader.readLine()) != null) {
                    int index = -1;
                    String[] arr = line.split("\\s+");
                    for (int i = 0; i < arr.length; i++) {
                        if (arr[i].equals(mmcblk)) {
                            index = i;
                            break;
                        }
                    }
                    if (index > 1) {
//                        Log.e(TAG, "size  = " + arr[index - 1]);
                        memorySize = Double.parseDouble(arr[index - 1]);
                        break;
                    }


                }
            } catch (IOException e) {

            } finally {
                if (bufferedReader != null) {
                    try {
                        bufferedReader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

//        double size = memorySize / 1024 / 1024;
//        DebugLog.e("memorySize = " + size + "GB");
        return memorySize / 1024 / 1024;
    }

    public static String getRomNominalSize() {

        String size = String.format("%.2f", (getRomTotalSize()));
//        DebugLog.e("get memory size: " + size + "GB");
        return size + " GB";
    }

    ///
    public static float getDefinedRamSize() {
        float size = EinkProperty.getRamSize();
        if (size < 0) {
            size = HtfyunBuildConfig.RAM_SIZE_MIN;
        }

        return size;
    }

    private final static float IFLYTEK_RPO_ROM_MIN = 57.0f;

    public static boolean isIflytekProByCheckRom() {
        Double romTotalSize = getRomTotalSize();
        Double romMin = Double.valueOf("32.50");
        int ret = romTotalSize.compareTo(romMin);
        return ret > 0;
    }

    public static float getDefinedRomSize() {
        float size = EinkProperty.getRomSize();
        if (size < 0) {
            size = HtfyunBuildConfig.ROM_SIZE_MIN;
        }

        if (isIflytekProByCheckRom()) {
            size = IFLYTEK_RPO_ROM_MIN;
        }

        return size;
    }

}
